package com.mybed.crm.controller;

import com.mybed.crm.anno.Log;
import com.mybed.crm.pojo.dto.*;
import com.mybed.crm.pojo.pojo.Business;
import com.mybed.crm.pojo.pojo.BusinessRecord;
import com.mybed.crm.pojo.vo.BusinessRecordVo;
import com.mybed.crm.pojo.vo.BusinessVo;
import com.mybed.crm.result.PageResult;

import com.mybed.crm.result.Result;
import com.mybed.crm.result.ResultP;
import com.mybed.crm.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/business")
@Slf4j
//商机管理
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 分页查询
     * @param pageBusinessDto
     * @return
     */
    @GetMapping("/list")
    public PageResult pageBusinessQuery(PageBusinessDto pageBusinessDto,@RequestParam("params[beginCreateTime]") String begin,
                                        @RequestParam("params[endCreateTime]") String end ){
        log.info(String.valueOf(pageBusinessDto));
        if (begin != "" && end != ""){
            pageBusinessDto.setBeginCreateTime(LocalDate.parse(begin));
            pageBusinessDto.setEndCreateTime(LocalDate.parse(end));
        }
        PageResult pageResult = businessService.pageBusinessQuery(pageBusinessDto);
        return pageResult;
    }

    /**
     * 公海池的分页查询
     * @param pageBusinessDto
     * @return
     */
    @GetMapping("/pool")
    public PageResult pageBusinessSeaQuery(PageBusinessDto pageBusinessDto){
        log.info(String.valueOf(pageBusinessDto));
        PageResult pageResult = businessService.pageBusinessSeaQuery(pageBusinessDto);
        return pageResult;
    }

    /**
     * 新增商机
     * @param businessDto
     * @return
     */
    @PostMapping
    public Result addBusiness(@RequestBody BusinessDto businessDto){
        log.info(String.valueOf(businessDto));
        businessService.addBusiness(businessDto);
        return Result.success();
    }

    /**
     * 根据id进行查询
     * @return
     */
    @GetMapping("/{id}")
    public Result selectBusinessId(@PathVariable Long id){
        Business business = businessService.selectBusinessId(id);
        return Result.success(business);
    }

    /**
     * 跟进更改商机数据
     * @param businessRecordDto
     * @return
     */
    @PostMapping("/record")
    public Result addRecord(@RequestBody BusinessRecordDto businessRecordDto){
        businessService.addRecord(businessRecordDto);
        return Result.success();
    }

    /**
     * 调用跟进查询回显结果
     * @param businessId
     * @return
     */
    @GetMapping("/record/list")
    public Result selectRecord(Long businessId){
        List<BusinessRecordVo> businessRecordVos = businessService.selectRecord(businessId);
        return Result.success(businessRecordVos);
    }

    /**
     * 踢回公海
     * @param id
     * @param backReason
     * @return
     */
    @PutMapping("/back/{id}/{backReason}")
    public Result updateBusinessStatus(@PathVariable Integer id, @PathVariable Integer backReason){
        businessService.updateBusinessStatus(id,backReason);
        return Result.success();
    }

    /**
     * 商机分配
     * @return
     */
    @Log("商机分配")
    @PutMapping("/assignment")
    public Result updateBusinessUserId(@RequestBody AssignmentDto assignmentDto){
        businessService.updateBusinessUserId(assignmentDto);
        return Result.success("全部分配");
    }
    /**捞出商机公海池
     * @param gainDto
     * @return
     */
    @PutMapping("/gain")
    public Result gainCluesPool(@RequestBody GainDto gainDto){
        log.info("要捞取的商机们为：{}",gainDto.getIds());
        businessService.gainCluesPool(gainDto);
        return Result.success();
    }
}
